package com.application.metroscanner;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.application.metroscanner.fragments.RideGroupGraphicFragment;
import com.application.metroscanner.fragments.RideGroupListFragment;
import com.application.metroscanner.tools.Classifier;
import com.example.metroscanner.R;

public class GraphicsActivity extends Activity implements
		RideGroupListFragment.OnItemSelectedListener {

	ListView groupList;
	public static final String SELECTED_CLUSTER = "SelectedCluster";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_graphics);
		// Make sure we're running on Honeycomb or higher to use ActionBar APIs
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			// Show the Up button in the action bar.
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}

		if (findViewById(R.id.fragmentContainer) != null) {

			// if we're being restored from a previous state
			if (savedInstanceState != null) {
				return;
			}

			// else create a new Fragment to be placed in the activity layout
			RideGroupListFragment firstFragment = new RideGroupListFragment();

			// In case this activity was started with special instructions from
			// an
			// Intent, pass the Intent's extras to the fragment as arguments
			firstFragment.setArguments(getIntent().getExtras());

			// Add the fragment to the 'fragment_container' FrameLayout
			getFragmentManager().beginTransaction()
					.add(R.id.fragmentContainer, firstFragment).commit();
		}
	}

	public void onRideGroupeSelected(Classifier.Cluster iCluster) {
		// Create fragment and give it an argument for the selected article
		RideGroupGraphicFragment newFragment = new RideGroupGraphicFragment();
		Bundle args = new Bundle();
		args.putSerializable(SELECTED_CLUSTER, iCluster);
		newFragment.setArguments(args);

		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();

		// Replace whatever is in the fragment_container view with this
		// fragment,
		// and add the transaction to the back stack so the user can navigate
		// back
		transaction.replace(R.id.fragmentContainer, newFragment);
		transaction.addToBackStack(null);

		// Commit the transaction
		transaction.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.graphics, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
